package io.renren.common.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import io.renren.common.constant.UrlConstant;
import io.renren.modules.generator.entity.RestEntity;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.UUID;

public class RestStripUtils {
    private static final String restStripUrl = UrlConstant.dirUrl;

    String createImage(String fileLocation, BufferedImage image) {
        String picUrl = "";
        String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
        try {
            fileLocation += fileName;
            FileOutputStream fos = new FileOutputStream(fileLocation);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            encoder.encode(image);
            picUrl = UrlConstant.fileUrl + fileName;
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return picUrl;
    }

    public String graphicsGeneration(RestEntity restEntity) {
        //实际数据行数+标题+备注
        int totalrow = 4;
        int totalcol = 5;
        int imageWidth = 2024;
        int imageHeight = totalrow * 40 + 20;
        int rowheight = 40;
        int startHeight = 10;
        int startWidth = 10;
        int colwidth = (int) ((imageWidth - 20) / totalcol);
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BufferedImage image;

        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        graphics.setColor(new Color(220, 240, 240));

        //画横线
        for (int j = 0; j < totalrow; j++) {
            graphics.setColor(Color.black);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5, startHeight + (j + 1) * rowheight);
        }
        //末行
        graphics.setColor(Color.black);
        graphics.drawLine(startWidth, imageHeight - 90, imageWidth - 5, imageHeight - 90);


        //画竖线
        for (int k = 0; k < totalcol; k++) {
            graphics.setColor(Color.black);
            graphics.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth, imageHeight - 50);
        }
        //末列
        graphics.setColor(Color.black);
        graphics.drawLine(imageWidth - 5, startHeight + rowheight, imageWidth - 5, imageHeight - 50);

        //设置字体
        Font font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);

        //写标题
        String title = "学生请假单";
        graphics.drawString(title, imageWidth / 3 + startWidth, startHeight + rowheight - 10);

        font = new Font("华文楷体", Font.BOLD, 18);
        graphics.setFont(font);

        //写入表头
        String[] headCells = {"学生姓名", "请假原因", "开始时间", "结束时间", "请假发起时间"};
        for (int m = 0; m < headCells.length; m++) {
            graphics.drawString(headCells[m].toString(), startWidth + colwidth * m + 5, startHeight + rowheight * 2 - 10);
        }

        //设置字体
        font = new Font("华文楷体", Font.PLAIN, 16);
        graphics.setFont(font);
        String[][] cellsValue = {{restEntity.getRestName(), restEntity.getRestReason(), sformat.format(restEntity.getStartTime()), sformat.format(restEntity.getEndTime()), sformat.format(restEntity.getRestTime())}};
        //写入内容
        for (int n = 0; n < cellsValue.length; n++) {
            String[] arr = cellsValue[n];
            for (int l = 0; l < arr.length; l++) {
                graphics.drawString(cellsValue[n][l].toString(), startWidth + colwidth * l + 5, startHeight + rowheight * (n + 3) - 10);
            }
        }

        font = new Font("华文楷体", Font.BOLD, 18);
        graphics.setFont(font);
        graphics.setColor(Color.RED);

        //写备注
        String remark = "此图片可作为请假凭据，请保存至本地！";
        graphics.drawString(remark, startWidth, imageHeight - 30);

        return createImage(restStripUrl, image);
    }
}

